/**    
 * @{#} My_StoreActivityDetatilActivity.java Create on 2014-6-17 上午10:36:09    
 *    
 * Copyright (c) 2013 by mark_zhu. 
 * @author <a href="mailto:zjc198805@163.com">mark_zhu</a>   
 * @version 1.0    
 */    
package com.mycjj.android.activity;

import java.util.Map;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.bean.BaseBean;
import com.mycjj.android.bean.StoreActivity;
import com.mycjj.android.bean.StoreActivityDetailBaseBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

//首页4s店活动
public class My_StoreActivityDetatilActivity extends BaseActivity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_me_store_activity_detail);
		initHeader();
		init();
		initData();
	}
	
	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("活动详情");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				My_StoreActivityDetatilActivity.this.finish();
			}
		});
	}
	
	private TextView tv_title,tv_time,tv_store_name,tv_content,tv_address;
	private ImageView iv_pic;
	private TextView textView4;
	private Button sign;
	private void init(){
		Intent intent = getIntent();
		if(intent != null){
			id = intent.getStringExtra("id");
		}
		tv_title = (TextView)findViewById(R.id.tv_title);
		tv_time = (TextView)findViewById(R.id.tv_time);
		tv_content = (TextView)findViewById(R.id.tv_content);
		iv_pic = (ImageView)findViewById(R.id.iv_pic);
		tv_address = (TextView)findViewById(R.id.tv_address);
		this.textView4 = (TextView)findViewById(R.id.textView4);
		sign = (Button) findViewById(R.id.sign);
		final Button dial = (Button) findViewById(R.id.dial);
		sign.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dosign();
			}
		});
		dial.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(null != dial.getTag())
				{
					String phone = dial.getTag().toString();
					if((null != phone) && (phone.length() > 0))
					{
						Intent intent = new Intent("android.intent.action.CALL", Uri  
			                    .parse("tel:" + phone));             
			            startActivity(intent);  
			            return;
					}
				}
				Toast.makeText(My_StoreActivityDetatilActivity.this, "号码未设置", Toast.LENGTH_LONG).show();
			}
		});
//		webView = (WebView)findViewById(R.id.webview);
//		webView.getSettings().setDefaultTextEncodingName("UTF-8");
//		webView.setBackgroundColor(0); 
	}
	private String id;
	StoreActivity bean;
	private void initData(){
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("client_id",ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		params.put("activity_id", id);
		request.requestStoreGetActivityDetailWithDoneHandler("", params, new AsyncHttpResponseHandler(){
			
			@Override
			public void onSuccess(String content) {
				StoreActivityDetailBaseBean baseBean = dataManager.getStoreActivityDetailBaseBean();
				if(baseBean != null){
					if(baseBean.getCode() == 200){
						bean = baseBean.getData();
						if(bean != null){
							tv_title.setText(bean.getActivity_title());
							tv_time.setText(bean.getActivity_start_date()+"-"+bean.getActivity_end_date());
							tv_address.setText(bean.getActivity_address());
							imageLoader.displayImage(bean.getActivity_banner(), iv_pic);
							tv_content.setText(bean.getActivity_desc());
							textView4.setText("已有"+bean.getActivity_sign()+"人报名");
							textView4.setTag(bean.getActivity_sign());
//							webView.loadDataWithBaseURL("",bean.getArticle_content(), "text/html", "UTF-8","");
						}
					}else{
						Toast.makeText(My_StoreActivityDetatilActivity.this, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}else{
					Log.e("mark", "ArticleDetailBaseBean is null!");
				}
			}
		});
	}
	
	private void dosign()
	{
		request = new ProjectHttpRequestImpl(this, false);
		RequestParams params = new RequestParams();
		params.put("activity_id", bean.getId());
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("token",
				Preferences.getSharedPreferences(this, "user_token", ""));
		request.requestStoreAddSignWithDoneHandler("", params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				
				BaseBean<Map> baseBean = (BaseBean<Map>)dataManager.getBaseBean();
				String message = baseBean.getMsg();
				if(baseBean.getCode() == 200){
					message = "报名成功";
					sign.setClickable(false);
					if(null != My_StoreActivityDetatilActivity.this.textView4.getTag()){
						int number = 0;
						try{
							number = Integer.parseInt(My_StoreActivityDetatilActivity.this.textView4.getTag().toString());
						}catch(Exception e){
							number = 0;
						}
						textView4.setText("已有"+(number+1)+"人报名");
					}
				}
				Toast.makeText(My_StoreActivityDetatilActivity.this, message, Toast.LENGTH_LONG).show();
				
				
			}
		});
	}
}
